﻿// //  ********************************************************************************
// //  Copyright (C) 2016 Sebastian Kenter. All rights reserved.
// // 
// //  Name    : MultiServerEngine | ServerFramework | ClientPeerFactory.cs
// //  Created : 2016 - 02 - 16 20:53
// //  Modified: 2016 - 02 - 20 12:32
// //  ********************************************************************************

using ExitGames.Logging;
using Photon.SocketServer;
using ServerFramework.Implementation.Server;
using ServerFramework.Interfaces.Application;
using ServerFramework.Interfaces.Client;
using ServerFramework.Interfaces.Configuration;
using ServerFramework.Interfaces.Handler;
using ServerFramework.Interfaces.Server;

namespace ServerFramework.Implementation.Client
{
    public class ClientPeerFactory : IClientPeerFactory
    {
        public ILogger Logger { get; }

        private IServerApplication Server { get; }
        private IClientPeerRegistry<IClientPeer> ClientPeerRegistry { get; }
        private IServerPeerRegistry<IServerPeer> ServerPeerRegistry { get; }
        private ITelegramHandlerRegistry HandlerRegistry { get; }

        public ClientPeerFactory(ILogger logger, IServerApplication server,
                                 IClientPeerRegistry<IClientPeer> clientPeerRegistry,
                                 IServerPeerRegistry<IServerPeer> serverPeerRegistry,
                                 ITelegramHandlerRegistry handlerRegistry)
        {
            Logger = logger;
            Server = server;
            ClientPeerRegistry = clientPeerRegistry;
            ServerPeerRegistry = serverPeerRegistry;
            HandlerRegistry = handlerRegistry;
        }

        public T CreatePeer<T>(IPeerConfig peerConfig) where T : class
        {
            // Is the peer the client wants to connect to the master server and the port is 4520?
            if (Server.GetIsMasterPeer() && peerConfig.GetPeerConfig<InitRequest>().LocalPort == 4520)
            {
                Logger.InfoFormat("About to create a new sub server peer on {0}", Server.ServerType);

                // If so then we have a request from a sub server
                return
                    new ServerPeer(peerConfig.GetPeerConfig<InitRequest>().Protocol,
                        peerConfig.GetPeerConfig<InitRequest>().PhotonPeer, Logger, Server, ServerPeerRegistry,
                        HandlerRegistry) as T;
            }

            // Is the peer the client wants to connect to the master server and the port is not 4520?
            if (Server.GetIsMasterPeer() && peerConfig.GetPeerConfig<InitRequest>().LocalPort != 4520)
            {
                Logger.InfoFormat("About to create a new client peer on {0}", Server.ServerType);
                // If so then we have a request from a real client
                return
                    new ClientPeer(peerConfig.GetPeerConfig<InitRequest>(), Logger, Server, ClientPeerRegistry,
                        HandlerRegistry) as T;
            }

            // If none of the cases match then we have a invalid request that must be declined.
            return null;
        }
    }
}
